using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Neovolve.Windows.Forms.Controls;
using Infragistics.Win.UltraWinGrid;

namespace RLFtrClc
{
    public partial class ChooseFighters : WizardPage
    {
        private string _fighterType;
        public Fighters FighterGroup;
        private int _maxFighters = 0;
        private int _declaredFighterCount = 0;
        private string _removeFighterName;

        public ChooseFighters(string FighterType, Fighters FighterGroup)
        {
            _fighterType = FighterType;
            this.FighterGroup = FighterGroup;
            InitializeComponent();
            init();
        }

        private void gridFighters_InitializeLayout(object sender, EventArgs eventArgs)
        {
            gridFighters.Rows.Band.Columns["Number"].MaxValue = _maxFighters;
        }


        private void gridFighters_BeforeCellUpdate(object sender, Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs eventArgs)
        {
            int newValue = 0;
            int oldValue = 0;

            if (eventArgs.Cell.Value != null)
            {
                Int32.TryParse(eventArgs.Cell.Value.ToString(), out oldValue);
                _declaredFighterCount -= oldValue;
                removeFighters(eventArgs.Cell.Row.Cells["Name"].Text);
            }
            Int32.TryParse(eventArgs.NewValue.ToString(), out newValue);
            if (_declaredFighterCount + newValue > _maxFighters)
            {
                eventArgs.Cancel = true;
                MessageBox.Show("You have chosen a value which exceeds the max count of allowable fighters for a " + _fighterType, "Error in fighter count.", MessageBoxButtons.OK);
            }
            else
            {
                _declaredFighterCount += newValue;
                addFighters(eventArgs.Cell.Row, newValue);
            }
        }

        private void removeFighters(string name)
        {
            _removeFighterName = name;
            FighterGroup.RemoveAll(NamedFighter);
        }

        private bool NamedFighter(Fighter fighter)
        {
            if (fighter.Name == _removeFighterName) {
                return true;
            } else {
                return false;
            }
        }

        private void addFighters(UltraGridRow row, int fighterCount)
        {
            for (int i = 0; i < fighterCount; i++)
            {
                string name = row.Cells["Name"].Text;
                int armor;
                Int32.TryParse(row.Cells["Armor"].Text, out armor);
                int thrust;
                Int32.TryParse(row.Cells["Thrust"].Text, out thrust);
                int cra;
                Int32.TryParse(row.Cells["CRA"].Text, out cra);
                int lra;
                Int32.TryParse(row.Cells["LRA"].Text, out lra);
                int missiles;
                Int32.TryParse(row.Cells["Missiles"].Text, out missiles);
                Fighter fighter = new Fighter(name, armor, thrust, cra, lra, missiles);
                FighterGroup.Add(fighter);
            }

            updateFighterCount(FighterGroup.Count);
        }

        private void init()
        {
            FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/Data/Fighters.uds", FileMode.Open);
            ultraDataSource1.LoadFromXml(fs);
            fs.Close();
            gridFighters.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(gridFighters_InitializeLayout);
            gridFighters.BeforeCellUpdate += new Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventHandler(gridFighters_BeforeCellUpdate);

            switch (_fighterType)
            {
                case "Squadron":
                    _maxFighters = 6;
                    break;
                case "Flight":
                    _maxFighters = 24;
                    break;
                case "Group":
                    _maxFighters = 72;
                    break;
            }

            updateFighterCount(0);
        }

        private void updateFighterCount(int fighterCount)
        {
            lblFighterCount.Text = "Total Fighter Count: " + fighterCount.ToString() + " of " + _maxFighters.ToString();
        }
    }

   
}